package unit4.part3;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Date;
import java.util.Random;

public class Demo {

    public static void main(String[] args) throws ParseException {
        //4.3.1.日期类Date
        Date dateA = new Date();
        Date dateB = new Date(System.currentTimeMillis() + 2);
        System.out.println(dateA);// 默认英文格式输出
        System.out.println(dateA.getTime());// 时间戳，毫秒级
        System.out.println(dateA.compareTo(dateB));// 日期对比，大于1，等于0，小于-1

        //4.3.2.日期格式化类SimpleDateFormat
        String format = "yyyy年MM月dd日 HH:mm:ss SSS";
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat(format);
        String str = simpleDateFormat.format(dateB);// date => str
        Date dateC = simpleDateFormat.parse(str);// str => date
        System.out.println(str);
        System.out.println(dateB.compareTo(dateC));

        //4.3.3.随机类Random
        Random random = new Random();// 默认种子数的随机对象，跟时间相关，同时考虑并发时累积，保障种子唯一
        System.out.println(random.nextInt());// MinInt - MaxInt
        System.out.println(random.nextInt(10));// 0 - 9
        System.out.println(random.nextDouble());// 0 - 1

        //4.3.4.统计年龄
        System.out.println(getAdultCount(getAges()));

        //4.3.5.String
        String strA = new String("abc");
        String strB = "abc";
        System.out.println(strA.equals(strB));// 内容相同
        System.out.println(strA == strB);// 引用不同
        strA = "abc";
        System.out.println(strA == strB);// 引用相同
        // 其他功能
        System.out.println(strA.length());// 长度
        System.out.println(strA.toCharArray());// 获取全部字符，存储在数组中
        System.out.println(strA.charAt(0));// 获取第一个字符
        char[] target = new char[3];
        strA.getChars(0, 3, target, 0);// 截取部分字符
        System.out.println(Arrays.toString(target));
        System.out.println(strA.substring(0, 3));// 截取字符串
        System.out.println(Arrays.toString(strA.split("b")));// 以b分割字符串
    }

    public static int[] getAges() {
        Random random = new Random();
        int[] ages = new int[200];
        for (int i = 0; i < 200; i++) {
            ages[i] = random.nextInt(61);
        }
        return ages;
    }

    public static int getAdultCount(int[] ages) {
        int num = 0;
        for (int x : ages) {
            if (x > 18) {
                num++;
            }
        }
        return num;
    }
}
